package com.fz.service.system.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.common.GlobalAll;
import com.fz.common.ResponseEntity;
import com.fz.dto.system.dictionaryValue.DictionaryValueAdd;
import com.fz.dto.system.dictionaryValue.DictionaryValueQuery;
import com.fz.dto.system.dictionaryValue.DictionaryValueUpd;
import com.fz.entity.system.DictionaryValue;
import com.fz.mapper.system.DictionaryValueMapper;
import com.fz.service.system.DictionaryValueService;
import com.fz.utils.JacksonUtils;
import com.fz.utils.StringUtils;
import com.fz.vo.system.dictionaryValue.DictionaryValueVo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author :xmj
 * @date :2021/3/24 上午9:43
 */
@Service
@CacheConfig(cacheNames = "dictionaryValue")
@SuppressWarnings("unchecked")
public class DictionaryValueServiceImpl implements DictionaryValueService {

    @Resource
    private DictionaryValueMapper dictionaryValueMapper;

    /**
     * @param pageNum              pageNum
     * @param pageSize             pageSize
     * @param dictionaryValueQuery dictionaryValueQuery
     * @return com.fz.common.ResponseEntity<com.fz.vo.system.dictionaryValue.DictionaryValueVo>
     * @description 字典值分页查询加入缓存
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    @Cacheable
    public ResponseEntity<DictionaryValueVo> findDictionaryValueList(Integer pageNum, Integer pageSize, DictionaryValueQuery dictionaryValueQuery) {
        Page<DictionaryValue> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DictionaryValue> lambdaQueryWrapper = new LambdaQueryWrapper<DictionaryValue>() {
            {
                //TODO 查询条件条件添加 ->在此拼接
                eq(DictionaryValue::getDeleteStatus, GlobalAll.DELETE_STATUS_NO)
                        .and(StringUtils.isNotEmpty(dictionaryValueQuery.getBlurry()),
                                o -> o.eq(dictionaryValueQuery.getBlurry() != null, DictionaryValue::getDictionaryValue, dictionaryValueQuery.getBlurry())
                                        .or()
                                        .eq(dictionaryValueQuery.getBlurry() != null, DictionaryValue::getDictionaryKey, dictionaryValueQuery.getBlurry())
                        );
            }
        };
        IPage<DictionaryValueVo> dictionaryValueList = dictionaryValueMapper.findDictionaryValueList(page, lambdaQueryWrapper);
        return ResponseEntity.buildSuccess(dictionaryValueList);
    }


    /**
     * @param dictionaryValueAdd dictionaryValueAdd
     * @return com.fz.common.ResponseEntity<com.fz.vo.system.dictionaryValue.DictionaryValueVo>
     * @description 更新字典值
     * @params
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<DictionaryValueVo> saveDictionaryValue(DictionaryValueAdd dictionaryValueAdd) {
        DictionaryValue dictionaryValue = JacksonUtils.obj2pojo(dictionaryValueAdd, DictionaryValue.class);
        return ResponseEntity.buildSuccess(dictionaryValueMapper.insert(dictionaryValue));
    }

    /**
     * @param dictionaryValueUpd dictionaryValueUpd
     * @return com.fz.common.ResponseEntity<com.fz.vo.system.dictionaryValue.DictionaryValueVo>
     * @description 更新字典值
     * @params
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<DictionaryValueVo> updateDictionaryValue(DictionaryValueUpd dictionaryValueUpd) {
        DictionaryValue dictionaryValue = JacksonUtils.obj2pojo(dictionaryValueUpd, DictionaryValue.class);
        return ResponseEntity.buildSuccess(dictionaryValueMapper.updateById(dictionaryValue));
    }

    /**
     * @param ids  ids
     * @return com.fz.common.ResponseEntity<com.fz.vo.system.dictionaryValue.DictionaryValueVo>
     * @description 删除字典值
     * @author xumj
     * @date 2021/3/26
     */
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<DictionaryValueVo> delDictionaryValue(String ids) {
        Integer[] integers = Convert.toIntArray(ids);
        LambdaQueryWrapper<DictionaryValue> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(DictionaryValue::getId, Arrays.asList(integers));
        dictionaryValueMapper.delete(queryWrapper);
        return ResponseEntity.buildSuccess(null);
    }

    /**
     * @param dictionaryId dictionaryId
     * @return java.util.List<com.fz.vo.system.dictionaryValue.DictionaryValueVo>
     * @description 根据字典ID查询对应的字典值
     * @author xumj
     * @date 2021/3/26
     */
    @Override
    @Cacheable(value = "#p0")
    public List<DictionaryValueVo> selectDictionaryValueList(Integer dictionaryId) {
        LambdaQueryWrapper<DictionaryValue> queryWrapper = new LambdaQueryWrapper<>();
        //TODO 查询条件条件添加 ->在此拼接
        queryWrapper.eq(DictionaryValue::getDeleteStatus, GlobalAll.DELETE_STATUS_NO).eq(DictionaryValue::getDictionaryId, dictionaryId);
        return dictionaryValueMapper.selectDictionaryValueList(queryWrapper);
    }
}
